home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14329 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.6 KB  |  161 lines

  1. Path: calvin.stemnet.nf.ca!jdeeley
  2. From: jdeeley@calvin.stemnet.nf.ca (J.Deeley)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Understanding pointers
  5. Date: Sat, 13 Apr 1996 11:41:09 -0400
  6. Organization: I need to be organized?
  7. Message-ID: <4kohlc$k62@coranto.ucs.mun.ca>
  8. References: <Barron.K.Herrington.21.01DF12CE@tek.com>
  9. NNTP-Posting-Host: calvin.stemnet.nf.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: MacSOUP 1.0d7
  14.  
  15. Barron Herrington <Barron.K.Herrington@tek.com> wrote:
  16.  
  17. > I need help understanding pointers, I understand variables and have the
  18. > concept of pointers, but I can't seem to get it into code.  Any quick
  19. > explanation or easy projects or easy short program examples would be greatly
  20. > appreciated.
  21.  
  22.  
  23. Not to rejoice in your misery, but I am glad that I am not the only one
  24. having this problem. I reread the chapter on pointers about 5 times.
  25. Like you, I understand the _concept_ very well, but have a bit of
  26. trouble implementing it! I just bought another C text book but I just
  27. got it and have been reviewing other stuff in it (interesting how
  28. different books approach the same problems!) and haven't got to pointers
  29. in it, yet. I am hoping it will make it even clearer.
  30.  
  31. Here is an easy program which may help make how to use a simple pointer
  32. a little clearer:
  33.  
  34. #include <stdio.h>
  35.  
  36.  
  37. /***********************/
  38. /* Function Prototypes */
  39. /***********************/
  40. void    SquareIt( int  number, int      *squarePtr );
  41.  
  42.  
  43. int     main( void )
  44. {
  45.         int     square; 
  46.                                          
  47.         SquareIt( 5, &square );         
  48.                                          
  49.         printf( "5 squared is %d.\n", square );
  50.  
  51.         return 0;
  52. }
  53.  
  54.  
  55. void    SquareIt( int  number, int      *squarePtr )
  56. {
  57.         *squarePtr = number * number;
  58. }
  59.  
  60.  
  61. My (limited because I am new to this too, so take this with a grain of
  62. salt!) understanding is that you need to use the address of operator (&)
  63. and  pointers (*) in order to be able to change the information the
  64. address contains, or to share the data between functions, since there
  65. are only three ways to do that.
  66.  
  67. One way to share data between functions is by using global variables
  68. (memory hungry)
  69. another is to use the return function (limited by only being able to
  70. return one peice of data)
  71. and the third way is by passing parameters by using values (can't give
  72. returns except as above) or addresses (as in the above code example)
  73. which you can then change and therefore return data with by using a
  74. pointer (bingo!) When you look at it that way, you can see why pointers
  75. are so essential to C! In a way, you have no choice but use them.
  76.  
  77. Or, as this paragraph from my text puts it:
  78.  
  79. Since passing parameters by value is a one-way operation, there╣s no way
  80. to get data back from the called function. Why would you ever want to?
  81. Several reasons. You might write a function that takes an employee
  82. number as a parameter. You might want that function to return the
  83. employee╣s salary in another parameter. How about a function that turns
  84. yards into meters? You could pass the number of yards as a value
  85. parameter, but how would you get back the number of meters? Passing a
  86. parameter by address (instead of by value) solves this problem. If you
  87. pass the address of a variable, the receiving function can use the *
  88. operator to change the value of the original variable.
  89.  
  90. quote ends.
  91.  
  92. In the code case above, we need to be able to pass back to the main
  93. function the info from mulitplying number and number. We could do this
  94. by using return in this example, but that would hardly let us learn
  95. about pointers! 
  96.  
  97. So, in main, the parameter list of SquareIt  passes an int (5) and the
  98. address of the variable "square" to the function SquareIt. But since
  99. that won't get the info back to main, we use *squarePtr in the function
  100. parameter list to be able to change the information that &square has in
  101. it.
  102.  
  103.  Pointers (like *squarePtr) are special because they HOLD the ADDRESS of
  104. another variable! In other words, *squarePtr is holding the ADDRESS of
  105. square. Which means it can change what square has in it! All from the
  106. comfort of another function. <lazy pointers> *grin*
  107.  
  108.  One thing that always messes me up is why do we need both & and *? It
  109. would be a lot simpler if we only used one of those symbols to do the
  110. same thing. So I think of it this way; the & operator is the real
  111. accessor to the variables address. It acts like an open door. "Here I
  112. am" it says, "change me!"
  113.  
  114.  The pointer just literally points at the variable, like a finger. It
  115. sort of says (imagine a finger called *squarePtr pointing at the
  116. variable address of square in main) "That's where I am going to put this
  117. information!!" and thanks to the address of's (&) open door policy,
  118. *squarePtr can. 
  119.  
  120. Why use *squarePtr instead of square in SquareIt?
  121. It would not do any good to just use the variable address  "square" in
  122. the function SquareIt, because as soon as SquareIt exits, the data  in a
  123. regular address is lost, no longer there. Has no effect on main. We need
  124. to change it permanantly so that main can have it. That's why we use a
  125. pointer. So now when main goes to printf "5 squared is" square now holds
  126. the info that number times number produced, thanks to using the address
  127. of operator and the pointer.
  128.  
  129.  
  130.  It seems to me from observation that a general rule is that you -pass-
  131. the pointers to the called function using the & (address of) operator
  132. when they are in a parameter list (opening the door), but when you are
  133. declaring functions (such as SquareIt in the above example) you use the
  134. * operator. (pointing to where to put the info in the open door) I think
  135. as long as we follow this general rule, we may be safe for now? <grin>
  136.  
  137. I think another thing that had me confused for a while is the fact that
  138. you can rename the pointers. At first I thought they should have the
  139. same name as what they pointed to. For instance, in the above example,
  140. &square can become *squarePtr. But when you figure that all they do is
  141. POINT to the address they are changing it makes more sense.
  142.  
  143. Anyway, what I did was use this general code format (as above) and write
  144. a whole bunch of little programs that returned addition, subtraction,
  145. etc. instead of square numbers. it made me feel a little better about
  146. the whole thing ;) So maybe you could try something similar. Just write
  147. some little programs
  148.  
  149. I hope this long ramble has helped. If anyone wants to comment or add to
  150. our understanding, feel free. I need all the help I can get. I have
  151. already said that I am a newbie to, so I would also appreciate not being
  152. flamed if my attempt to help is not perfect.
  153.  
  154.  
  155. JD
  156.  
  157. ---------------------
  158. Caution...Newbie Programmer on Board!
  159. --
  160.  
  161.